<?php

declare (strict_types=1);

namespace app\bbxw\controller;


use think\admin\Controller;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;
/**
 * 兑换订单管理
 * @class Package
 * @package app\bbxw\controller
 */
class ExchangeOrder extends Controller
{

    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'exchange_order';
    /**
     * 商品管理
     * @auth true
     * @menu true
     * @login true
     */
    public function index()
    {

        $this->title = '商品管理';
        $query = $this->_query($this->table);
        // 列表排序并显示
        $query->like('customer_name,goods_name')->equal('goods_id')->order('id desc')->page();

        $this->fetch();
    }

    /**
     * 图文列表数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(array &$data)
    {

    }

    public function remove()
    {
        $id = input('id');
        $list = $this->app->db->name($this->table)->whereIn('id', $id)->select()->toArray();
        foreach ($list as $item){
            //恢复库存
            $this->app->db->name('goods')->where('id',$item['goods_id'])->inc('stock')->update();
            if($item['type'] == 1){
                //返回积分
                $this->app->db->name('customer')->where('id',$item['user_id'])->dec('use_points',(float)$item['amount'])->update();
            }else{
                $this->app->db->name('power_source')->where('id',$item['user_id'])->dec('use_points',(float)$item['amount'])->update();
            }
            $this->app->db->name($this->table)->where('id',$item['id'])->delete();
        }
        $this->success('删除成功');

    }




}